package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SizeUtils
import com.lv.common.imp.OnPositionMore
import com.lv.common.imp.OnPositionTListener
import com.lv.common.widget.recyclerview.decoration.SpacesItemDecoration
import com.lv.service.R
import com.lv.service.adapter.BookAddressAdapter
import com.lv.service.bean.AreasBean
import com.lv.service.bean.CityBean
import com.lv.service.bean.CommonTab
import com.lv.service.bean.ScreeningBean
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class BookHZSelectedPopupView(
    private val mContext: Context,
    private val mAllBeans: List<ScreeningBean>? = null,
    private var mProvincePosition: Int = 0,
    private var mCityPosition: Int = 0,
    private var mAreaPosition: Int = 0,
    private var mTownPosition: Int = 0,
    private val mPositionListener: OnPositionMore<String>? = null
) :
    BottomPopupView(mContext) {
    private val current = 0
    private val name = ""
    private var rvProvince: RecyclerView? = null
    private var rvCity: RecyclerView? = null
    private var rvArea: RecyclerView? = null
    private var rvTown: RecyclerView? = null
    private var tvProvince: TextView? = null
    private var tvCity: TextView? = null
    private var tvArea: TextView? = null
    private var tvTown: TextView? = null
    private var tvSubmit: TextView? = null

    private var viewTop: View? = null
    private val mBeans = mutableListOf<ScreeningBean>()
    private val mProvinceBeans = mutableListOf<CommonTab>()
    private val mCityBeans = mutableListOf<CommonTab>()
    private val mAreaBeans = mutableListOf<CommonTab>()
    private val mTownBeans = mutableListOf<CommonTab>()
    private var mProvinceAdapter: BookAddressAdapter? = null
    private var mCityAdapter: BookAddressAdapter? = null
    private var mAreaAdapter: BookAddressAdapter? = null
    private var mTownAdapter: BookAddressAdapter? = null
    override fun getImplLayoutId(): Int {
        return R.layout.book_hz_selected_popup
    }

    override fun onCreate() {
        super.onCreate()
        rvProvince = findViewById<RecyclerView>(R.id.rv_province)
        tvProvince = findViewById<TextView>(R.id.tv_province)
        rvCity = findViewById<RecyclerView>(R.id.rv_city)
        tvCity = findViewById<TextView>(R.id.tv_city)
        rvArea = findViewById<RecyclerView>(R.id.rv_area)
        tvArea = findViewById<TextView>(R.id.tv_area)
        rvTown = findViewById<RecyclerView>(R.id.rv_town)
        tvTown = findViewById<TextView>(R.id.tv_town)

        tvSubmit = findViewById<TextView>(R.id.tv_submit)
        viewTop = findViewById<View>(R.id.view_top)
        findViewById<ImageView>(R.id.iv_close).setOnClickListener {
            mPositionListener?.onClick(
                -1, -1, -1, -1, data = null
            )
            dismiss()
        }

        initView()
        initEvnet()
    }

    private fun initView() {
        mAllBeans?.let {
            mBeans.clear()
            mBeans.addAll(mAllBeans)
        }
        val provinceLayoutManager = GridLayoutManager(context, 3)
        val cityLayoutManager = GridLayoutManager(context, 3)
        val areaLayoutManager = GridLayoutManager(context, 3)
        val townLayoutManager = GridLayoutManager(context, 3)
        rvProvince?.layoutManager = provinceLayoutManager
        rvCity?.layoutManager = cityLayoutManager
        rvArea?.layoutManager = areaLayoutManager
        rvTown?.layoutManager = townLayoutManager
        rvCity?.isFocusable = false
        rvArea?.isFocusable = false
        tvTown?.isFocusable = false
        mProvinceBeans.clear()
        mProvinceBeans.add(CommonTab(name = "全部地区", isSelcted = false))
        for (i in 0 until mBeans.size) {
            val screeningBean = mBeans[i]
            val commonTab = CommonTab(name = screeningBean.provin, isSelcted = false)
            mProvinceBeans.add(commonTab)
        }
        mProvinceBeans[mProvincePosition].isSelcted = true
        mProvinceAdapter = BookAddressAdapter(mProvinceBeans)
        rvProvince?.adapter = mProvinceAdapter
        rvProvince?.addItemDecoration(
            SpacesItemDecoration(
                SizeUtils.dp2px(24.0f),
                SizeUtils.dp2px(0.0f),
                false
            )
        )

        mCityAdapter = BookAddressAdapter(mCityBeans)
        rvCity?.adapter = mCityAdapter
        rvCity?.addItemDecoration(
            SpacesItemDecoration(
                SizeUtils.dp2px(24.0f),
                SizeUtils.dp2px(0.0f),
                false
            )
        )
        mAreaAdapter = BookAddressAdapter(mAreaBeans)
        rvArea?.adapter = mAreaAdapter
        rvArea?.addItemDecoration(
            SpacesItemDecoration(
                SizeUtils.dp2px(24.0f),
                SizeUtils.dp2px(0.0f),
                false
            )
        )
        mTownAdapter = BookAddressAdapter(mTownBeans)
        rvTown?.adapter = mTownAdapter
        rvTown?.addItemDecoration(
            SpacesItemDecoration(
                SizeUtils.dp2px(24.0f),
                SizeUtils.dp2px(0.0f),
                false
            )
        )

        if (mProvincePosition != 0) {
            val city = mBeans[mProvincePosition - 1].city
            initCity(city)
            if (mCityPosition != 0) {
                val area = mBeans[mProvincePosition - 1].city[mCityPosition - 1].areas
                initArea(area)
                if (mAreaPosition != 0) {
                    val town =
                        mBeans[mProvincePosition - 1].city[mCityPosition - 1].areas[mAreaPosition - 1].prefecture
                    if (!town.isNullOrEmpty()) {
                        initTown(town)
                    }
                }
            }
        }

    }

    private fun initEvnet() {
        mProvinceAdapter?.mPositionListener = OnPositionTListener { position, data ->
            if (position == mProvincePosition) {
                return@OnPositionTListener
            }
            mProvinceBeans[mProvincePosition].isSelcted = false
            mProvinceAdapter?.notifyItemChanged(mProvincePosition)
            mProvinceBeans[position].isSelcted = true
            mProvinceAdapter?.notifyItemChanged(position)
            mProvincePosition = position
            mCityPosition = 0
            mAreaPosition = 0
            mTownPosition = 0
            if (position == 0) {
                tvCity?.visibility = GONE
                rvCity?.visibility = GONE
            } else {
                val city = mBeans[position - 1].city
                initCity(city)
            }
            tvArea?.visibility = GONE
            rvArea?.visibility = GONE
            tvTown?.visibility = GONE
            rvTown?.visibility = GONE
        }
        mCityAdapter?.mPositionListener = OnPositionTListener { position, data ->
            if (position == mCityPosition) {
                return@OnPositionTListener
            }
            mCityBeans[mCityPosition].isSelcted = false
            mCityAdapter?.notifyItemChanged(mCityPosition)
            mCityBeans[position].isSelcted = true
            mCityAdapter?.notifyItemChanged(position)
            mCityPosition = position
            mAreaPosition = 0
            mTownPosition = 0
            if (position == 0) {
                tvArea?.visibility = GONE
                rvArea?.visibility = GONE
            } else {
                val area = mBeans[mProvincePosition - 1].city[mCityPosition - 1].areas
                initArea(area)
            }
            tvTown?.visibility = GONE
            rvTown?.visibility = GONE
        }
        mAreaAdapter?.mPositionListener = OnPositionTListener { position, data ->
            if (position == mAreaPosition) {
                return@OnPositionTListener
            }
            mAreaBeans[mAreaPosition].isSelcted = false
            mAreaAdapter?.notifyItemChanged(mAreaPosition)
            mAreaBeans[position].isSelcted = true
            mAreaAdapter?.notifyItemChanged(position)
            mAreaPosition = position
            mTownPosition = 0
            if (position == 0) {
                tvTown?.visibility = GONE
                rvTown?.visibility = GONE
            } else {
                val town =
                    mBeans[mProvincePosition - 1].city[mCityPosition - 1].areas[mAreaPosition - 1].prefecture
                if (!town.isNullOrEmpty()) {
                    initTown(town)
                }
            }
        }
        mTownAdapter?.mPositionListener = OnPositionTListener { position, data ->
            if (position == mTownPosition) {
                return@OnPositionTListener
            }
            mTownBeans[mTownPosition].isSelcted = false
            mTownAdapter?.notifyItemChanged(mTownPosition)
            mTownBeans[position].isSelcted = true
            mTownAdapter?.notifyItemChanged(position)
            mTownPosition = position
        }

        viewTop?.setOnClickListener {
            mPositionListener?.onClick(
                -1, -1, -1, -1, data = null
            )
            dismiss()
        }
        tvSubmit?.setOnClickListener {
            var name = ""
            if (mProvincePosition == 0) {
                name = "全部地区"
            } else {
                if (mCityPosition == 0) {
                    name = mProvinceBeans[mProvincePosition].name ?: ""
                } else {
                    if (mAreaPosition == 0) {
                        name = mCityBeans[mCityPosition].name ?: ""
                    } else {
                        if (mTownPosition == 0) {
                            name = mAreaBeans[mAreaPosition].name ?: ""
                        } else {
                            name = mTownBeans[mTownPosition].name ?: ""
                        }
                    }
                }
            }
            mPositionListener?.onClick(
                mProvincePosition,
                mCityPosition,
                mAreaPosition,
                mTownPosition,
                data = name
            )
            dismiss()
        }

    }

    private fun initTown(town: List<String>) {
        mTownBeans.clear()
        mTownBeans.add(CommonTab(name = "全部地区", isSelcted = false))
        town.forEach { item ->
            val commonTab = CommonTab(name = item, isSelcted = false)
            mTownBeans.add(commonTab)
        }
        mTownBeans[mTownPosition].isSelcted = true
        mTownAdapter?.notifyDataSetChanged()
        tvTown?.visibility = VISIBLE
        rvTown?.visibility = VISIBLE
    }

    private fun initArea(area: List<AreasBean>) {
        mAreaBeans.clear()
        mAreaBeans.add(CommonTab(name = "全部地区", isSelcted = false))
        area.forEach { item ->
            val commonTab = CommonTab(name = item.area, isSelcted = false)
            mAreaBeans.add(commonTab)
        }
        mAreaBeans[mAreaPosition].isSelcted = true
        mAreaAdapter?.notifyDataSetChanged()
        tvArea?.visibility = VISIBLE
        rvArea?.visibility = VISIBLE
    }

    private fun initCity(city: List<CityBean>) {
        mCityBeans.clear()
        mCityBeans.add(CommonTab(name = "全部地区", isSelcted = false))
        city.forEach { item ->
            val commonTab = CommonTab(name = item.city, isSelcted = false)
            mCityBeans.add(commonTab)
        }
        mCityBeans[mCityPosition].isSelcted = true
        mCityAdapter?.notifyDataSetChanged()
        tvCity?.visibility = VISIBLE
        rvCity?.visibility = VISIBLE
    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()
        mPositionListener?.onClick(current, data = null)

    }

}